package com.dddd.blog.bloguser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dddd.blog.api.user.pojo.User;
import com.dddd.blog.blogcommon.pojo.vo.UserListVO;
import com.dddd.blog.blogcommon.utils.CollectionUtils;
import com.dddd.blog.bloguser.mapper.UserMapper;
import com.dddd.blog.bloguser.service.IUserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author dddd
 * @since 2022-02-27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource(type = UserMapper.class)
    private UserMapper userMapper;

    /**
     * 根据id查找用户
     * @param id
     * @return
     */
    @Override
    public User getUserById(Long id) {
        return userMapper.selectById(id);
    }


    @Override
    public List<Map<String, Object>> getAuthorList() {

        return null;
    }

    @Override
    public List<Map<String, Object>> getAuthorListPage(long currentPage, long pageSize) {
        return null;
    }


    /**
     * 将Author添加到User数据库
     * @param map
     * @return
     */
    @Override
    public int addUserWithAuthor(Map<String, Object> map) {

        return userMapper.addUserWithAuthor(map);

    }


    /**
     * 将Author添加到User数据库
     * @param mapList
     * @return
     */
    @Override
    public int addUserWithAuthors(List<Map<String, Object>> mapList) {

        return userMapper.addUserWithAuthors(mapList);

    }

    /**
     * @return
     */
    @Override
    public Integer getUserCount() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.select("id");
        Integer count = userMapper.selectCount(wrapper);
        return count;
    }

    @Override
    public List<UserListVO> getUserList() {

        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.select(User.class,i -> !"password".equals(i.getColumn()) &&
                                       !"salt".equals(i.getColumn()) &&
                                       !"birthday".equals(i.getColumn()) &&
                                       !"uuid".equals(i.getColumn()));
        List<User> list = userMapper.selectList(wrapper);

        List<UserListVO> collect = null;
        try {
            collect = CollectionUtils.collectionInvert(list, UserListVO.class);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return collect;
    }

    @Override
    public List<UserListVO> getUserList(@Param("currentPage") Integer currentPage,
                                        @Param("pageSize") Integer pageSize)
    {
        Page<User> page = new Page(currentPage,pageSize);

        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.select(User.class,i -> !"password".equals(i.getColumn()) &&
                !"salt".equals(i.getColumn()) &&
                !"birthday".equals(i.getColumn()) &&
                !"uuid".equals(i.getColumn()));

        Page<User> userPage = userMapper.selectPage(page, wrapper);

        // getRecords()获取分页数据
        List<UserListVO> list;
        try {
             list = CollectionUtils.collectionInvert(userPage.getRecords(), UserListVO.class);
             return list;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public User getUserByName(String name) {

        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.eq("username",name);

        User user = userMapper.selectOne(wrapper);

        return user;
    }

}
